var express = require('express');
var router = express.Router();

const usersService = require('../servise/usersServise');
const userService = require('../service/userService');
const jwt = require("jsonwebtoken");
const { secretKey, sha256 } = require('../utils/token')
/* GET users listing. */
// 管理员登录接口
router.post('/admin', async function (req, res) {
  let { username, pwd } = req.body;
  console.log(req.body);
  let data = await usersService.admin(username, sha256(pwd));
  console.log(Object.keys(data).length);
  if (Object.keys(data).length > 0) {
    // req.session.user = data;
    let authToken = jwt.sign({ user: data }, secretKey);
    res.send({ token: authToken });
    return;
  }
  res.send(data);
})

// 用户登录接口
// 登录输入 手机号+密码
// 登录失败，返回 {}
router.post('/login', async function (req, res) {
  let phoneNum = req.body.phoneNum
  let pwd = req.body.pwd
  let data = await userService.login(phoneNum, sha256(pwd));
  console.log(data)
  if (Object.keys(data).length > 0) {
    // 生成token
    let authToken = jwt.sign({ user: data }, secretKey);
    // 将生成的token返回
    res.send({ token: authToken });
    return;
  }
  res.send({})
})

// 注册用这个接口
// 增加用户
router.post('/', async function (req, res) {
  let { username, pwd, phoneNum, email, priv } = req.body
  priv ? 0 : priv = 'user'
  let data = await userService.reg({
    username: username,
    pwd: sha256(pwd),
    phoneNum: phoneNum,
    email: email,
    priv: priv
  })
  res.send(data)
})

// 注册时，检查是否重名
// 传入参数 {username: xxx} 或 {phoneNum: xxx} 
// 重名返回 {isFind: 1} 不重名返回 {isFind: 0}
router.get('/reg', async function (req, res) {
  let isFind = await userService.proving(req.query)
  res.send({ isFind })
})

// 查询全部用户
router.get('/', async function (req, res) {
  console.log(req.query)
  let { type, value, current, size } = req.query
  let searchObj = {}
  let data
  if (type && value) {
    if (type == 'username') {
      //searchObj[type] = value
      searchObj[type] = { $regex: value }
      data = await userService.find(searchObj, { current: current || 1, size: size || 5 })
    } else if (type == 'phoneNum') {
      searchObj[type] = value
      data = await userService.find(searchObj, { current: current || 1, size: size || 5 })
    } else if (type == 'priv') {
      searchObj[type] = value
      data = await userService.find(searchObj, { current: current || 1, size: size || 5 })
    }
  }
  if (Object.keys(searchObj).length == 0) {
    data = await userService.find(searchObj, { current: current || 1, size: size || 3 })
  }
  res.send(data)
})

// 根据ID查询用户
router.get('/:id', async function (req, res) {
  let id = req.params.id;
  let data = await userService.findById(id);
  res.send(data)
})

// 修改用户
router.put('/:id', async function (req, res) {
  let id = req.params.id
  let data = await userService.change(id, req.body)
  res.send(data)
})

// 删除用户
router.delete('/:id', async function (req, res) {
  let id = req.params.id
  let data = await userService.delete(id)
  res.send(data)
})

// console.log(sha256('123'));
module.exports = router;
